package com.example.lvsuo.service.cxg.impl;

import com.example.lvsuo.entity.cxg.vo.FlowDataLine;
import com.example.lvsuo.entity.cxg.vo.FlowNameVo;
import com.example.lvsuo.mapper.cxg.FlowLineMapper;
import com.example.lvsuo.service.cxg.IFlowLineService;
import org.apache.commons.collections.ListUtils;
import org.apache.velocity.runtime.directive.Foreach;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Service
@Transactional
public class IFlowLineServiceImpl implements IFlowLineService {


    @Autowired
    FlowLineMapper flowLineMapper;


    @Override
    public void addLines(Map<String, Object> map) {

        ArrayList list = (ArrayList) map.get("list");

        List flowlines = new ArrayList();

        /*
null[1,2,3,4,5]null

null  1  2      1
1    2   3      2
2    3   4      3
3    4   5      4
4    5   null   5

         */


        System.out.println("当前数据"+list);

        for (int i = 1; i <= list.size(); i++) {


                Map lineMap = new HashMap();

            if (i==1 && list.size()==1){// 如果只有一个节点审批人
                lineMap.put("fid",map.get("fid"));
                lineMap.put("last",null);
                lineMap.put("current",list.get(i-1));
                lineMap.put("next",null);
                lineMap.put("column",i);
                flowlines.add(lineMap);
                break;
            }


                if (i==1){ // 如果审批人不止一个
                    lineMap.put("fid",map.get("fid"));
                    lineMap.put("last",null);
                    lineMap.put("current",list.get(i-1));
                    lineMap.put("next",list.get(i));
                    lineMap.put("column",i);
                    flowlines.add(lineMap);
                }
                if(i!=1 && i<list.size()){
                    lineMap.put("fid",map.get("fid"));
                    lineMap.put("last",list.get(i-2));
                    lineMap.put("current",list.get(i-1));
                    lineMap.put("next",list.get(i));
                    lineMap.put("column",i);
                    flowlines.add(lineMap);
                }
                if(i==list.size()){
                    lineMap.put("fid",map.get("fid"));
                    lineMap.put("last",list.get(i-2));
                    lineMap.put("current",list.get(i-1));
                    lineMap.put("next",null);
                    lineMap.put("column",i);
                    flowlines.add(lineMap);
                }


            }
//        System.out.println("处理后数据"+flowlines);
        flowLineMapper.addLines(flowlines);
        }

    @Override
    public void delLineByFid(String fid) {
        flowLineMapper.delLineByFid(fid);
    }

    @Override
    public List<FlowDataLine> queryLineByid(String fid) {
        return flowLineMapper.queryLineByid(fid);
    }

    @Override
    public void updateLine(Map<String, Object> map) {
        flowLineMapper.delLineByFid(map.get("fid").toString()); // 先删除原有的流程线

        // 再添加流程洗
        this.addLines(map);

    }


}
